package com.shinhye.smartsms.utils;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;

public class MyAsyncQueryHandler extends AsyncQueryHandler {
	OnChangeCursorListener listener;

	public MyAsyncQueryHandler(ContentResolver cr) {
		super(cr);
	}

	@Override
	protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
		if(listener != null) {
			listener.preFunc(token, cookie, cursor);
		}
		
		if(cookie != null && cookie instanceof CursorAdapter){
			CursorAdapter adapter = (CursorAdapter) cookie;
			adapter.changeCursor(cursor);
		}
		CommonUtils.printCursor(cursor);
		
		if(listener != null) {
			listener.afterFunc(token, cookie, cursor);
		}
	}
	
	public void setOnChangeCursorListener(OnChangeCursorListener listener) {
		this.listener = listener;
	}
	
	/**
	 * An Interface to indicate what to do before and after change the adapter's cursor.
	 * @author ShinHye
	 *
	 */
	public interface OnChangeCursorListener {
		/**
		 * What to do before the cursor change.
		 */
		void preFunc(int token, Object cookie, Cursor cursor);
		/**
		 * What to do after the cursor change.
		 */
		void afterFunc(int token, Object cookie, Cursor cursor);
	}
	
}
